<?php
class Logger{
	const TYPE_APPEND = 1;
	const TYPE_REWRITE = 2;
	private $f = false;
	private $stopped = false;
	private $contens = array();
	function __construct($name, $type = self::TYPE_APPEND){
		switch ($type){
			case self::TYPE_APPEND :{
				$this->f = fopen(_cfg("path.logger")."/".$name.".log", "a");
				break;		
			}
			case self::TYPE_REWRITE :{
				$this->f = fopen(_cfg("path.logger")."/".$name.".log", "w+");
				break;		
			}
			default:{
				$this->stopped = true;
				break;
			}
		}
	}
	
	function __destruct(){
		if ($this->f)
			fclose($this->f);
	}
	
	function msg($msg){
		if ($this->stopped)
			return;
		$trace = debug_backtrace();
		if ($trace[2]['function'] == "require_once")
			$sender = basename($trace[2]['args'][0]);
		elseif (isset($trace[2]['class']))
			$sender = $trace[2]['class']."::".$trace[2]['function'];
		else 
			$sender = basename($trace[2]['file'])."::".$trace[2]['function'];
		fwrite($this->f,date("[Y-m-d H:m:s]", time())." ".$sender.": ".$msg."\r\n");
		$this->contens[] = date("[Y-m-d H:m:s]", time())." ".$sender.": ".$msg."\r\n";
	}
	
	function get()
	{
		return $this->contens;
	}
}

?>